package com.backend.werewolfkillingbackend.contorller;

import com.backend.werewolfkillingbackend.common.R;
import com.backend.werewolfkillingbackend.pojo.Message;
import com.backend.werewolfkillingbackend.service.MessageService;
import com.backend.werewolfkillingbackend.service.PlayerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/private/v1")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private PlayerService playerService;

    @GetMapping("/message/{roomId}")
    public R<List<Message>> getRoomMessage(@PathVariable String roomId) {

        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Message::getRoomId, roomId);

        List<Message> messageList = messageService.list(queryWrapper);

        for (Message message : messageList) {
            if (message.getPlayerId() != null) {
                message.setPlayer(playerService.getById(message.getPlayerId()));
            }
        }

        return R.success(messageList, "获取成功");
    }
}
